from rest_framework import serializers

from goods.models import Goods, GoodsCategory, GoodsAlbum

class ASerializer(serializers.ModelSerializer):
    """商品子类别"""
    class Meta(object):
        model = GoodsCategory
        fields = ('id', 'title')


class SubGoodsCategorySerializer(serializers.ModelSerializer):
    """商品子类别"""
    parent = ASerializer(read_only=True)        # 关联对象（一个）： 父类别

    class Meta(object):
        model = GoodsCategory
        fields = ('id', 'title', 'parent')


class AlbumSerializer(serializers.ModelSerializer):
    """商品图片（Album： 图库）"""
    class Meta(object):
        model = GoodsAlbum
        fields = ('id', 'thumb_path', 'original_path')


class GoodsSerializer(serializers.ModelSerializer):
    # 关联对象（多个）： 商品图片
    goodsalbum_set = AlbumSerializer(many=True, read_only=True)
    # 关联对象（一个）： 商品所属类别
    category = SubGoodsCategorySerializer()

    class Meta(object):
        model = Goods
        fields = '__all__'


class GoodsCategorySerializer(serializers.ModelSerializer):
    """商品类别"""
    goodscategory_set = SubGoodsCategorySerializer(label='子类别', read_only=True, many=True)

    parent = ASerializer(read_only=True)

    class Meta(object):
        model = GoodsCategory
        fields = ("id", "title", "goodscategory_set", "parent")


# # 以下代码来自组长
class ImageGoodsSerializer(serializers.ModelSerializer):
    """商品图片数据序列化器"""
    class Meta:
        #model 指明要序列化的模型类
        model = GoodsAlbum
        fields = ('id', 'thumb_path','original_path')
        read_only_fields = ('id', 'thumb_path', 'original_path')

class OneCategoryGoodsSerializer(serializers.ModelSerializer):
    """商品类别数据序列化器"""
    class Meta:
        #model 指明要序列化的模型类
        model = GoodsCategory
        fields = ('id', 'create_time','update_time','title','sort_id','parent')
        read_only_fields = ('id', 'create_time','update_time','title','sort_id','parent')


class TwoCategoryGoodsSerializer(serializers.ModelSerializer):
    """商品类别数据序列化器"""
    parent = OneCategoryGoodsSerializer(read_only=True)
    class Meta:

        #model 指明要序列化的模型类
        model = GoodsCategory
        fields = ('id', 'title','parent')
        read_only_fields = ('id', 'title')

#
# class ParticularsGoodsSerializer(serializers.ModelSerializer):
#     """商品详情数据序列化器"""
#
#     goodsalbum_set = ImageGoodsSerializer(many=True,read_only=True)
#     category = TwoCategoryGoodsSerializer(read_only=True)
#     class Meta:
#         # model 指明要序列化的模型类
#         model = Goods
#         fields = (
#         'id', 'img_url', 'create_time', 'update_time', 'title', 'zhaiyao', 'content', 'status', 'is_red', 'is_slide',
#         'sub_title', 'goods_no', 'stock', 'sales', 'market_price', 'sell_price','goodsalbum_set','category')
#
#         read_only_fields = (
#         'id', 'img_url', 'create_time', 'update_time', 'title', 'zhaiyao', 'content', 'status', 'is_red', 'is_slide',
#         'sub_title', 'goods_no', 'stock', 'sales', 'market_price', 'sell_price')
#
# class RecommandGoodsSerializer(serializers.ModelSerializer):
#     """推荐商品数据序列化器"""
#     category = TwoCategoryGoodsSerializer(read_only=True)
#
#     class Meta:
#         # model 指明要序列化的模型类
#         model = Goods
#         fields = (
#             'id', 'img_url', 'create_time', 'update_time', 'title', 'zhaiyao', 'content', 'status', 'is_red',
#             'is_slide',
#             'sub_title', 'goods_no', 'stock', 'sales', 'market_price', 'sell_price', 'category')
#
#         read_only_fields = (
#             'id', 'img_url', 'create_time', 'update_time', 'title', 'zhaiyao', 'content', 'status', 'is_red',
#             'is_slide',
#             'sub_title', 'goods_no', 'stock', 'sales', 'market_price', 'sell_price')
# class CategoryGoodsSerializer(serializers.ModelSerializer):
#     class Meta:
#         # model 指明要序列化的模型类
#         model = GoodsCategory
#         fields = ('id', 'title')
#         read_only_fields = ('id', 'title')

